// -------------------------------------------------------------------------
//    @FileName			:    {Filename}
//    @Author           :    GaoYi
//    @Date             :    2017-09-27
//    @Module           :   {Filename}
//
// -------------------------------------------------------------------------

#pragma once

#include "NFIMsgHead.h"

class NFCMsgHead : public NFIMsgHead
{
public:
	NFCMsgHead()
	{
		munSize = 0;
		munMsgID = 0;
		timeTamp = 0;
		reserverd = 0;
	}

	enum
	{
		NF_HEAD_LENGTH = 16,
	};

	virtual int GetHeadLength()
	{
		return NF_HEAD_LENGTH;
	}

	// Message Head[ MsgID(4) | TimeTamp(4) | reserverd(4) | MsgSize(4) ]
	virtual int EnCode(char* strData)
	{
		uint32_t nOffset = 0;

		uint32_t nMsgID = NF_HTONL(munMsgID);
		memcpy(strData + nOffset, (void*)(&nMsgID), sizeof(munMsgID));
		nOffset += sizeof(munMsgID);

		uint32_t nTimeTamp = NF_HTONL(timeTamp);
		memcpy(strData + nOffset, (void*)(&nTimeTamp), sizeof(timeTamp));
		nOffset += sizeof(timeTamp);

		uint32_t nReserverd = NF_HTONL(reserverd);
		memcpy(strData + nOffset, (void*)(&nReserverd), sizeof(reserverd));
		nOffset += sizeof(reserverd);

		uint32_t nPackSize = munSize; //munSize + NF_HEAD_LENGTH;
		uint32_t nSize = NF_HTONL(nPackSize);
		memcpy(strData + nOffset, (void*)(&nSize), sizeof(munSize));
		nOffset += sizeof(munSize);

		if (nOffset != GetHeadLength())
		{
			assert(0);
		}

		return nOffset;
	}

	// Message Head[ MsgID(4) | TimeTamp(4) | reserverd(4) | MsgSize(4) ]
	virtual int DeCode(const char* strData)
	{
		uint32_t nOffset = 0;

		uint32_t nMsgID = 0;
		memcpy(&nMsgID, strData + nOffset, sizeof(munMsgID));
		munMsgID = NF_NTOHL(nMsgID);
		nOffset += sizeof(munMsgID);

		uint32_t nTimeTamp = 0;
		memcpy(&nTimeTamp, strData + nOffset, sizeof(timeTamp));
		timeTamp = NF_NTOHL(nTimeTamp);
		nOffset += sizeof(timeTamp);

		uint32_t nReserverd = 0;
		memcpy(&nReserverd, strData + nOffset, sizeof(reserverd));
		reserverd = NF_NTOHL(nReserverd);
		nOffset += sizeof(reserverd);

		uint32_t nPackSize = 0;
		memcpy(&nPackSize, strData + nOffset, sizeof(munSize));
		munSize = NF_NTOHL(nPackSize);//NF_NTOHL(nPackSize) - NF_HEAD_LENGTH;
		nOffset += sizeof(munSize);

		if (nOffset != GetHeadLength())
		{
			assert(0);
		}

		return nOffset;
	}

	virtual uint32_t GetMsgID() const
	{
		return munMsgID;
	}

	virtual void SetMsgID(uint32_t nMsgID)
	{
		munMsgID = nMsgID;
	}

	virtual uint32_t GetTimeTamp() const
	{
		return timeTamp;
	}

	virtual void SetTimeTamp(uint32_t nTimeTamp)
	{
		timeTamp = nTimeTamp;
	}

	virtual uint32_t GetReserverd() const
	{
		return reserverd;
	}

	virtual void SetReserverd(uint32_t nReserverd)
	{
		reserverd = nReserverd;
	}

	virtual uint32_t GetBodyLength() const
	{
		return munSize;
	}

	virtual void SetBodyLength(uint32_t nLength)
	{
		munSize = nLength;
	}
protected:
	uint32_t munMsgID;
	uint32_t timeTamp;
	uint32_t reserverd;
	uint32_t munSize;
};
